package com.laker.admin.module.excel.application.impl;

import com.laker.admin.module.excel.application.ExcelDataSourceApplication;
import com.laker.admin.module.excel.domain.ExcelDataSource;
import com.laker.admin.module.excel.repository.ExcelDataSourceRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;


/**
 * @author : nihongxin
 * @date : 2021/3/22
 */
@Component
public class ExcelDataSourceApplicationImpl implements ExcelDataSourceApplication {

    @Autowired
    private ExcelDataSourceRepository excelDataSourceRepository;


    @Override
    public ExcelDataSource save(ExcelDataSource excelDataSource) {
        return excelDataSourceRepository.save(excelDataSource);
    }

    @Override
    public ExcelDataSource getExcelDataSourceByCheck(ExcelDataSource excelDataSource) {
        Specification<ExcelDataSource> specification = (root, query, criteriaBuilder) -> criteriaBuilder.and(
                criteriaBuilder.equal(root.get("excelTemplateId"), excelDataSource.getExcelTemplateId()),
                criteriaBuilder.equal(root.get("excelTemplateSheetId"), excelDataSource.getExcelTemplateSheetId()),
                criteriaBuilder.equal(root.get("excelCoordinates"), excelDataSource.getExcelCoordinates()),
                criteriaBuilder.equal(root.get("excelFillId"), excelDataSource.getExcelFillId())
        );
        Optional<ExcelDataSource> optional = excelDataSourceRepository.findOne(specification);
        return optional.isPresent() ? optional.get() : null;
    }

    @Override
    public List<ExcelDataSource> getExcelDataSourceList(Long excelFillId) {
        Specification<ExcelDataSource> specification = (root, query, criteriaBuilder) -> criteriaBuilder.and(
                criteriaBuilder.equal(root.get("excelFillId"), excelFillId)
        );
        return excelDataSourceRepository.findAll(specification);
    }
}
